package custom;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Message {
	private int sender, reciever, id;
	private String msg;
	
	public Message(int sender, int reciever, String msg) throws SQLException{
		this.sender = sender;
		this.reciever = reciever;
		this.msg = msg;
		ResultSet rs = QuizDBC.getInstance().selectFromMessages(sender, reciever, msg, -1);
		rs.next();
		id = rs.getInt("id");
	}
	
	public int getSender(){
		return sender;
	}
	
	public int getReciever(){
		return reciever;
	}
	
	public int gtId(){
		return id;
	}
	
	public String getHtml(){
		return "";
	}
	
	public static void send(int sender, int reciever, String msg){
		QuizDBC.getInstance().insertMassage(sender, reciever, msg, 0);
	}
	
	/**
	 * mesijebis gverdze gamosatani siis htmls abrunebs
	 * @param id
	 * @return
	 */
	public static String getMessagedContactsHtml(int id){
		return "";
	}
	
	/**
	 * gibrunebs yvelastan nasaubreb messigebis vectors
	 * @param sender
	 * @param reciever
	 * @return
	 * @throws SQLException 
	 */
	public static ArrayList<Message> getConversationWith(int sender, int reciever) throws SQLException{
	/*	ResultSet rs = QuizDBC.getInstance().selectFromMessages(sender, reciever, "", -1);
		ArrayList<Message> list = new ArrayList<Message>();
		while(rs.next()){
			Message msg= new Message(rs.getInt("sender"), rs.getInt("reciever"), rs.getString("msg"));
			list.add(msg);
		}
		return list;
	*/	return new ArrayList<Message>();
	}
	
	public static boolean hasNewMessage(int id) throws SQLException{
//		ResultSet rs = QuizDBC.getInstance().selectFromMessages(-1, id, "", 0);
//		return rs.next();
		return true;
	}
	
	public static boolean hasNewMessageFrom(int id1, int id2) throws SQLException{
		ResultSet rs = QuizDBC.getInstance().selectFromMessages(id1, id2, "", 0);
		return rs.next();
	}
	
	/**
	 * new message-ebshi false-s mianichebs id-tvis
	 * @param id
	 */
	public static void removeNewMessages(int to, int from){
//		QuizDBC.getInstance().updateMessageAsSeen(id);
	}
	
}
